<template>
  <!-- class:body_div_container -->
  <div class="app-container ">
    <el-tabs value="1" type="card">
      <el-tab-pane label="帮助中心" name="1">
        <el-form :model="queryParams" ref="queryForm" size="medium" :inline="true" v-show="showSearch"
          label-width="60px">
          <el-form-item label="查询栏" prop="title">
            <el-input v-model="queryParams.title" placeholder="请输入帮助主题" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="操作人员" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入操作人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" placeholder="公告类型" clearable>
          <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-form :model="{}" size="medium" :inline="true" label-width="60px">
          <el-form-item label="操作栏" prop="" v-hasPermi="['system:notice:add']">
            <el-button type="primary" plain icon="el-icon-plus" size="medium" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
        <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="medium" @click="handleAdd"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="medium"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="medium"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
        >删除</el-button>
      </el-col> 
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 
    </el-row> -->

        <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="序号" align="center" min-width="60">
            <template slot-scope="scope">
              <span>{{ scope.$index + 1 || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="帮助主题" align="center" prop="title" min-width="150" :show-overflow-tooltip="true" />
          <el-table-column label="排序" align="center" prop="sort" min-width="100" />
          <el-table-column label="修改人" align="center" prop="updateBy" min-width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.updateBy || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="修改时间" align="center" prop="createTime" min-width="150">
            <template slot-scope="scope">
              <span>{{ scope.row.updateTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createBy" min-width="100" />
          <el-table-column label="状态" align="center" prop="status" min-width="100">
            <template slot-scope="scope">
              <dict-tag :options="statusHelp" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" min-width="150">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280" align="center" class-name="medium-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:notice:edit']">修改</el-button>
              <el-button v-if="scope.row.status === '1'" size="small" icon="el-icon-sort-up" type="success"
                @click="handleOnline(scope.row)">发布
              </el-button>
              <el-button v-else size="small" icon="el-icon-sort-down" type="danger" @click="handleOnline(scope.row)">下架
              </el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:notice:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="主题" prop="title">
              <el-input v-model="form.title" placeholder="请输入主题" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input-number style="width:20% ;" v-model="form.sort" :controls="false" :min="1" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择公告类型">
                <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_notice_status" :key="dict.value" :label="dict.value">
                  {{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <editor v-model="form.content" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice, updateNoticechangeStatus } from "@/api/system/notice";

export default {
  name: "Notice",
  // dicts: ['sys_notice_status', 'sys_notice_type'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        orderByColumn: 'helpId',
        isAsc: 'desc'//倒叙
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "必填", trigger: "blur" }
        ],
        content: [
          { required: true, message: "必填", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "必填", trigger: ["blur", 'change'] }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList () {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        helpId: undefined,
        title: undefined,
        content: undefined,
        sort: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.helpId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加主题";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const helpId = row.helpId
      getNotice(helpId).then(response => {
        this.form = response.data;
        const Base64 = require('js-base64').Base64
        this.form.content = Base64.decode(response.data.content)
        this.open = true;
        this.title = "修改主题";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = Object.assign({}, this.form)
          const Base64 = require('js-base64').Base64
          data.content = Base64.encode(data.content)
          if (this.form.helpId != undefined) {
            updateNotice(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const helpId = row.title
      this.$modal.confirm('是否确认删除主题为"' + helpId + '"的数据项？').then(function () {
        return delNotice(row.helpId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleOnline (row) {
      const helpId = row.status
      this.$modal.confirm(`是否确认${helpId === '0' ? '下架' : '发布'}主题为${row.title}的数据项？`).then(function () {
        let params = {
          helpId: row.helpId,
          status: helpId === '0' ? '1' : '0'
        }
        return updateNoticechangeStatus(params)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    }
  }
};
</script>
